//
// Created by wujehy on 2020/8/3.
//

#ifndef AUTH_RAFT_NETWORKWARPPER_H
#define AUTH_RAFT_NETWORKWARPPER_H

#include <handy/event_base.h>
#include "baseType.h"
#include "BusinessManager.h"

#include "NetWorkModule/NetWorkBase.h"

/**
 * @brief 网络封装层
 */
class NetWorkWarpper
{
    friend class BusinessManager;

protected:
    friend class NetWorkBase;

    BusinessManager m_businessManager; ///< 业务管理器
    handy::MultiBase *m_eventBase;
    TCPServer *tcpServer = nullptr;
    HttpServer *httpServer = nullptr;
    Client *client = nullptr;
    int thread_size = 2;
    mutable int m_client_id = 1;

    int tcpServerPort = 12345; ///< tcp 服务端id
    void initClient();

    void initTcpServer();

    void initHttpServer();

    void initCallbacks();

    handy::MsgCallBack onMessageCallback;
    handy::TcpCallBack onConnectCallback;
public:
    NetWorkWarpper();


    void init();

    virtual ~NetWorkWarpper();

    const handy::MsgCallBack &getOnMessageCallback() const;

    const handy::TcpCallBack &getOnConnectCallback() const;

    // 阻塞启动
    void loop();

    // 结束事件
    void stop();

    // 客户端链接 池
    std::map<int64_t, handy::TcpConnPtr> m_id_client_map;

    /**
     * @brief 设置 网络模块的线程数
     * @param threadSize
     */
    void setThreadSize(int threadSize);

    void call_init_success();

    void setTcpServerPort(int tcpServerPort);
};


#endif //AUTH_RAFT_NETWORKWARPPER_H
